package cn.aiyuan.controller;

import cn.aiyuan.common.annotation.OperationLogAnno;
import cn.aiyuan.common.enumerate.OperationTypeEnum;
import cn.aiyuan.entity.base.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author wy
 * @date 2020/11/10
 */

@Slf4j
@Api(value = "TestController", tags = "TestController")
@RestController
@RequestMapping("/api/v1/test")
public class TestController {

    @Resource
    RedisTemplate<String,Object> redisTemplate;

    @Resource
    RedissonClient redissonClient;

    @ApiOperation(value = "redis", notes = "redis")
    @GetMapping("/get")
    public JsonData getDictionaryTree() throws InterruptedException {

        RLock lock = redissonClient.getLock("lock");
        try {
            boolean b = lock.tryLock(20, -1, TimeUnit.SECONDS);
            log.info("boolean-->{}",b);
            if (b) {
                Long test = redisTemplate.opsForValue().increment("test");
                TimeUnit.SECONDS.sleep(1);
                log.info("testNum-->{}",test);
                return JsonData.ok(test);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (lock.isHeldByCurrentThread()){
                lock.unlock();
            }
        }

        return JsonData.ok();
    }
}
